package com.foreknow.dao.impl;

import com.foreknow.dao.PersonDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Person;

import java.sql.SQLException;
import java.util.List;

public class PersonDaoImpl extends BaseDao implements PersonDao {

    @Override
    public boolean insert(Person person) throws SQLException {
        int isRight = jdbcTemplate.update("insert into person(name,age,school,address) values(?,?,?,?)",
                person.getName(),person.getAge(),person.getSchool(),person.getAddress());
        return isRight == 1;
    }

    @Override
    public boolean delete(int id) throws SQLException {
        int isRight = jdbcTemplate.update("delete from person where id=?",id);
        return isRight == 1;
    }

    @Override
    public List<Object> queryAll() {
        EntityMapping entityMapping = mappingFactory.getMapping(MappingFactory.PERSON_MAPPING);
        List<Object> list = jdbcTemplate.query("select * from person",entityMapping);
        //        List<Person> newList = new ArrayList<>();
        //        for(int i = 0;i<list.size();i++) {
        //            Person person = (Person) list.get(i);
        //            newList.add(person);
        //        }
        return list;
    }

    @Override
    public Person queryById(int id) {
        EntityMapping entityMapping = mappingFactory.getMapping(MappingFactory.PERSON_MAPPING);
        List<Object> list = jdbcTemplate.query("select * from person where id=?",entityMapping,id);
        if(list.size() > 0) {
            return (Person) list.get(0);
        }
        return null;
    }

    @Override
    public boolean update(Person person) throws SQLException {
        return false;
    }



}
